package com.smartkey.utils;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.widget.Toast;

import com.smartkey.screenshot.ScreenShot;

public class ScreenShotUtil {

	/**
	 * 小米手机截屏API
	 * 
	 * @param context
	 */
	public static void screenShotXIAOMI(Context context) {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.CAPTURE_SCREENSHOT");
		context.sendBroadcast(intent);
	}

	/**
	 * 通用截屏代码 需Root
	 * 
	 * @param context
	 */
	public static void screentShotByRoot(Context context) {
		String photoDir = Environment.getExternalStorageDirectory()
				+ File.separator + Environment.DIRECTORY_DCIM;
		String fileName = DateUtils.getCurrentDateStr(DateUtils.format1)
				+ ".JPEG";
		String filePath = photoDir + File.separator + fileName;
		try {
			File file = new File(filePath);
			if(file.exists()){
				file.delete();
			}
			file.getParentFile().mkdirs();
			file.createNewFile();
			
			Bitmap bt = ScreenShot.getScreenBitmap(context);
			FileOutputStream out = new FileOutputStream(filePath);
			bt.compress(Bitmap.CompressFormat.PNG, 100, out);
			out.flush();
			out.close();
			Toast.makeText(context, "截屏成功", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
//			Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}
}
